"use strict";
layui.use(["okUtils", "okCountUp"], function () {
	
var data = {"monthTotalIncome":122331,
"pastSevenDateTotalIncome":[0,0,0,0,1233,132123,12513],
"monthNewAddMember":30,
"pastSevenDateNewAddMember":[2,10,4,5,5,4,0],
"monthNewAddUser":200,
"pastSevenDateNewAddUser":[20,30,20,30,70,20,10],
"monthNewAddChat":240,
"pastSevenDateNewAddChat":[30,30,30,40,40,30,30],
"pastSevenDateSignInCount":[100,120,130,110,90,81,51],
"yesterdayInCome":
		{"time":"2020-03-08",
		"memberCard":223,
		"privateLesson":12313,
		"featuredLesson":231,
		"rehabilitationLesson":232,
		"advertisement":200},	
"pastSevenDateMemberCard":[120,110,123,190,230,120,400],
"pastSevenDatePrivateLesson":[132,245,312,321,233,555,913],
"pastSevenDateFeaturedLesson":[210,120,150,250,330,123,520],
"pastSevenDateRehabilitationLesson":[110,230,450,210,310,220,110],
"pastSevenDateAdvertisement":[130,220,350,120,233,590,200]};
	
	
    var countUp = layui.okCountUp;
    var okUtils = layui.okUtils;
    var $ = layui.jquery;
    okLoading.close();
    
    //在获取页面时，进行ajax访问并获取info数值赋值入data
    $.ajax({
            cache: true,
            type: "POST",
            url:"getConsole",      
            async: false,
            error: function(request) {
                alert("失败!");
            },
            success: function(info) {
                for(var i=0;i<info.pastSevenDateTotalIncome.length;i++){
                    if(info.pastSevenDateTotalIncome[i]==null){
                        info.pastSevenDateTotalIncome[i]=0;
                    }
                }
            data = info;
            }
       });   
    /**
     * 总收入、新增会员数、健身圈数、注册用户
     */
    function initMediaCont() {
        var elem_nums = $(".media-cont .num");
        elem_nums.each(function (i, j) {
            var ran = parseInt(Math.random() * 1900 + 100);
            if(i==0){
        		ran = data.monthTotalIncome;
        	}
            if(i==1){
        		ran = data.monthNewAddMember;
        	}
            if(i==2){
        		ran = data.monthNewAddChat;
        	}
            if(i==3){
        		ran = data.monthNewAddUser;
        	}
            !new countUp({
                target: j,
                endVal: ran
            }).start();
        });
    }

    function dataTrendOption(color,a,b,c,d,e,f,g) {
        color = color || "#00c292";
        return {
            color: color, toolbox: {show: false, feature: {saveAsImage: {}}},
            grid: {left: '-1%', right: '0', bottom: '0', top: '5px', containLabel: false},
            xAxis: [{type: 'category', boundaryGap: false, splitLine: {show: false}, data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']}],
            yAxis: [{type: 'value', splitLine: {show: false}}],
            series: [{
                name: '用户', type: 'line', stack: '总量', smooth: true, symbol: "none", clickable: false, areaStyle: {},
                data: [a,b,c,d,e,f,g]
            }]
        }
    }

    function randomData() {
        return Math.round(Math.random() * 500);
    }
 /**
     * 近一周数量涨幅图表
     */ 
    function initDataTrendChart() {
        // 收入
        var echIncome = echarts.init($("#echIncome")[0]);
        // 商品
        var echGoods = echarts.init($('#echGoods')[0]);
        // 博客
        var echBlogs = echarts.init($("#echBlogs")[0]);
        // 用户
        var echUser = echarts.init($('#echUser')[0]);
        echIncome.setOption(dataTrendOption("#00c292",data.pastSevenDateTotalIncome[0],data.pastSevenDateTotalIncome[1],data.pastSevenDateTotalIncome[2],data.pastSevenDateTotalIncome[3],data.pastSevenDateTotalIncome[4],data.pastSevenDateTotalIncome[5],data.pastSevenDateTotalIncome[6]));
        echGoods.setOption(dataTrendOption("#ab8ce4",data.pastSevenDateNewAddMember[0],data.pastSevenDateNewAddMember[1],data.pastSevenDateNewAddMember[2],data.pastSevenDateNewAddMember[3],data.pastSevenDateNewAddMember[4],data.pastSevenDateNewAddMember[5],data.pastSevenDateNewAddMember[6]));
        echBlogs.setOption(dataTrendOption("#03a9f3",data.pastSevenDateNewAddChat[0],data.pastSevenDateNewAddChat[1],data.pastSevenDateNewAddChat[2],data.pastSevenDateNewAddChat[3],data.pastSevenDateNewAddChat[4],data.pastSevenDateNewAddChat[5],data.pastSevenDateNewAddChat[6]));
        echUser.setOption(dataTrendOption("#fb9678",data.pastSevenDateNewAddUser[0],data.pastSevenDateNewAddUser[1],data.pastSevenDateNewAddUser[2],data.pastSevenDateNewAddUser[3],data.pastSevenDateNewAddUser[4],data.pastSevenDateNewAddUser[5],data.pastSevenDateNewAddUser[6]));
        okUtils.echartsResize([echIncome, echGoods, echBlogs, echUser]);
    }

    var userActiveTodayChartOption = {
        tooltip: {},
        color: "#03a9f3",
        xAxis: {type: 'category', data: ['前五天', '前四天', '前三天', '前两天', '大前天', '前天', '昨日']},
        yAxis: {type: 'value'},
        series: [{data: [data.pastSevenDateSignInCount[0], data.pastSevenDateSignInCount[1], data.pastSevenDateSignInCount[2], data.pastSevenDateSignInCount[3], data.pastSevenDateSignInCount[4],data.pastSevenDateSignInCount[5], data.pastSevenDateSignInCount[6]], type: 'bar'}]
    };

    /**
     * 今日用户活跃量图表
     */
    function initUserActiveTodayChart() {
        var userActiveTodayChart = echarts.init($("#userActiveTodayChart")[0], "themez");
        userActiveTodayChart.setOption(userActiveTodayChartOption);
        okUtils.echartsResize([userActiveTodayChart]);
    }

    var userSourceTodayChartOption = {
        title: {show: false, text: '收入来源', subtext: '纯属虚构', x: 'center'},
        tooltip: {trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)"},
        legend: {orient: 'vertical', left: 'left', data: ['会员卡收入', '私教课收入', '特色课收入', '康复课收入', '广告收入']},
        series: [
            {
                name: '收入来源', type: 'pie', radius: '55%', center: ['50%', '60%'],
                data: [{value: data.yesterdayInCome.memberCard, name: '会员卡收入'}, {value: data.yesterdayInCome.privateLesson, name: '私教课收入'}, {value: data.yesterdayInCome.featuredLesson, name: '特色课收入'}, {value: data.yesterdayInCome.rehabilitationLesson, name: '康复课收入'}, {value: data.yesterdayInCome.advertisement, name: '广告收入'}],
                itemStyle: {emphasis: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)'}}
            }
        ]
    };

    /**
     * 今日用户访问来源图表
     */
    function initUserSourceTodayChart() {
        var userSourceTodayChart = echarts.init($("#userSourceTodayChart")[0], "themez");
        userSourceTodayChart.setOption(userSourceTodayChartOption);
        okUtils.echartsResize([userSourceTodayChart]);
    }

    var userSourceWeekChartOption = {
        title: {show: true, text: ''},
        tooltip: {trigger: 'axis', axisPointer: {type: 'cross', label: {backgroundColor: '#6a7985'}}},
        legend: {data: ['会员卡收入', '私教课收入', '特色课收入', '康复课收入', '广告收入']},
        toolbox: {show: false, feature: {saveAsImage: {}}},
        grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
        xAxis: [{type: 'category', boundaryGap: false, data: ['前五天', '前四天', '前三天', '前两天', '大前天', '前天', '昨日']}],
        yAxis: [{type: 'value', splitLine: {show: false},}],
        series: [
            {name: '会员卡收入', type: 'line', stack: '总量', smooth: true, areaStyle: {}, data: [data.pastSevenDateMemberCard[0], data.pastSevenDateMemberCard[1], data.pastSevenDateMemberCard[2], data.pastSevenDateMemberCard[3], data.pastSevenDateMemberCard[4], data.pastSevenDateMemberCard[5], data.pastSevenDateMemberCard[6]]},
            {name: '私教课收入', type: 'line', stack: '总量', smooth: true, areaStyle: {}, data: [data.pastSevenDatePrivateLesson[0], data.pastSevenDatePrivateLesson[1], data.pastSevenDatePrivateLesson[2], data.pastSevenDatePrivateLesson[3], data.pastSevenDatePrivateLesson[4], data.pastSevenDatePrivateLesson[5], data.pastSevenDatePrivateLesson[6]]},
            {name: '特色课收入', type: 'line', stack: '总量', smooth: true, areaStyle: {}, data: [data.pastSevenDateFeaturedLesson[0], data.pastSevenDateFeaturedLesson[1], data.pastSevenDateFeaturedLesson[2], data.pastSevenDateFeaturedLesson[3], data.pastSevenDateFeaturedLesson[4], data.pastSevenDateFeaturedLesson[5], data.pastSevenDateFeaturedLesson[6]]},
            {name: '康复课收入', type: 'line', stack: '总量', smooth: true, areaStyle: {normal: {}}, data: [data.pastSevenDateRehabilitationLesson[0], data.pastSevenDateRehabilitationLesson[1], data.pastSevenDateRehabilitationLesson[2], data.pastSevenDateRehabilitationLesson[3], data.pastSevenDateRehabilitationLesson[4], data.pastSevenDateRehabilitationLesson[5], data.pastSevenDateRehabilitationLesson[6]]},
            {name: '广告收入', type: 'line', stack: '总量', smooth: true, label: {normal: {show: true, position: 'top'}}, areaStyle: {normal: {}}, data: [data.pastSevenDateAdvertisement[0], data.pastSevenDateAdvertisement[1], data.pastSevenDateAdvertisement[2], data.pastSevenDateAdvertisement[3], data.pastSevenDateAdvertisement[4], data.pastSevenDateAdvertisement[5], data.pastSevenDateAdvertisement[6]]}
        ]
    };

    /**
     * 本周用户访问来源图表
     */
    function initUserSourceWeekChart() {
        var userSourceWeekChart = echarts.init($("#userSourceWeekChart")[0], "themez");
        userSourceWeekChart.setOption(userSourceWeekChartOption);
        okUtils.echartsResize([userSourceWeekChart]);
    }

    initMediaCont();
    initDataTrendChart();
    initUserActiveTodayChart();
    initUserSourceTodayChart();
    initUserSourceWeekChart();
});


